/*     */ package com.zimbra.cs.session;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LruMap;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.iochannel.CrossServerNotification;
/*     */ import com.zimbra.cs.iochannel.MessageChannel;
/*     */ import com.zimbra.cs.iochannel.MessageChannelException;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.util.LinkedList;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class RemoteSoapSession
/*     */   extends SoapSession
/*     */ {
/*     */   private ZimbraSoapContext authUserCtxt;
/*     */   
/*     */   public RemoteSoapSession(ZimbraSoapContext zsc)
/*     */   {
/*  41 */     super(zsc);
/*     */     try {
/*  43 */       this.authUserCtxt = new ZimbraSoapContext(zsc);
/*     */     }
/*     */     catch (ServiceException e) {}
/*     */   }
/*     */   
/*     */   public RemoteSoapSession register() throws ServiceException
/*     */   {
/*  50 */     super.register();
/*  51 */     if (MessageChannel.getInstance().isRunning()) {
/*  52 */       registerNotificationConnection(new CrossMailboxPushChannel(null));
/*     */     }
/*  54 */     return this;
/*     */   }
/*     */   
/*     */   protected boolean isMailboxListener()
/*     */   {
/*  59 */     return false;
/*     */   }
/*     */   
/*     */   public String getRemoteSessionId(Server server)
/*     */   {
/*  64 */     return null;
/*     */   }
/*     */   
/*     */   public void putRefresh(Element ctxt, ZimbraSoapContext zsc)
/*     */   {
/*  69 */     ctxt.addUniqueElement("refresh");
/*     */   }
/*     */   
/*     */ 
/*     */   public Element putNotifications(Element ctxt, ZimbraSoapContext zsc, int lastSequence)
/*     */   {
/*  75 */     if (ctxt == null) {
/*  76 */       return null;
/*     */     }
/*     */     SoapSession.QueuedNotifications ntfn;
/*  79 */     synchronized (this.sentChanges) {
/*  80 */       if (!this.changes.hasNotifications()) {
/*  81 */         return null;
/*     */       }
/*  83 */       ntfn = this.changes;
/*  84 */       this.changes = new SoapSession.QueuedNotifications(this, ntfn.getSequence() + 1);
/*     */     }
/*     */     
/*  87 */     putQueuedNotifications(null, ntfn, ctxt, zsc);
/*  88 */     return ctxt;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  97 */   private static final Map<String, LinkedList<String>> sentNotifications = new LruMap(100);
/*     */   
/*     */   private class CrossMailboxPushChannel
/*     */     implements SoapSession.PushChannel
/*     */   {
/*     */     private CrossMailboxPushChannel() {}
/*     */     
/*     */     public void closePushChannel() {}
/*     */     
/*     */     public int getLastKnownSequence()
/*     */     {
/* 108 */       return 0;
/*     */     }
/*     */     
/*     */     public ZimbraSoapContext getSoapContext()
/*     */     {
/* 113 */       return null;
/*     */     }
/*     */     
/*     */     public boolean localChangesOnly()
/*     */     {
/* 118 */       return false;
/*     */     }
/*     */     
/*     */     public boolean isPersistent()
/*     */     {
/* 123 */       return true;
/*     */     }
/*     */     
/*     */     public void notificationsReady()
/*     */     {
/*     */       CrossServerNotification ntfn;
/*     */       try {
/* 130 */         ntfn = CrossServerNotification.create(RemoteSoapSession.this, RemoteSoapSession.this.authUserCtxt);
/*     */       } catch (MessageChannelException e) {
/* 132 */         ZimbraLog.session.warn("unable to create CrossServerNotification", e);
/* 133 */         return;
/*     */       }
/* 135 */       if (!checkDuplicateNotification(ntfn)) {
/* 136 */         MessageChannel.getInstance().sendMessage(ntfn);
/*     */       }
/*     */     }
/*     */     
/*     */     private boolean checkDuplicateNotification(CrossServerNotification ntfn) {
/*     */       String msgHash;
/*     */       try {
/* 143 */         msgHash = ByteUtil.getDigest(ntfn.toString().getBytes("UTF-8"));
/*     */       } catch (UnsupportedEncodingException e) {
/* 145 */         msgHash = ByteUtil.getDigest(ntfn.toString().getBytes());
/*     */       }
/* 147 */       String accountId = RemoteSoapSession.this.authUserCtxt.getAuthtokenAccountId();
/*     */       LinkedList<String> messageHashes;
/* 149 */       synchronized (RemoteSoapSession.sentNotifications) {
/* 150 */         messageHashes = (LinkedList)RemoteSoapSession.sentNotifications.get(accountId);
/* 151 */         if (messageHashes == null) {
/* 152 */           messageHashes = new LinkedList();
/* 153 */           RemoteSoapSession.sentNotifications.put(accountId, messageHashes);
/*     */         }
/*     */       }
/* 156 */       synchronized (messageHashes) {
/* 157 */         if (!messageHashes.contains(msgHash))
/*     */         {
/* 159 */           if (messageHashes.size() > 9) {
/* 160 */             messageHashes.removeFirst();
/*     */           }
/* 162 */           messageHashes.add(msgHash);
/* 163 */           return false;
/*     */         }
/*     */       }
/* 166 */       return true;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/session/RemoteSoapSession.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */